package com.xuecheng.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.exception.XueChengPlusException;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.mapper.XcChooseCourseMapper;
import com.xuecheng.learning.mapper.XcCourseTablesMapper;
import com.xuecheng.learning.model.dto.MyCourseTableParams;
import com.xuecheng.learning.model.dto.XcChooseCourseDto;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.model.po.XcChooseCourse;
import com.xuecheng.learning.model.po.XcCourseTables;
import com.xuecheng.learning.service.MyCourseTablesService;
import com.xuecheng.model.PageResult;
import com.xuecheng.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.event.PaintEvent;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class MyCourseTablesServiceImpl implements MyCourseTablesService {
    @Autowired
    XcChooseCourseMapper xcChooseCourseMapper;
    @Autowired
    XcCourseTablesMapper xcCourseTablesMapper;
    @Autowired
    ContentServiceClient contentServiceClient;
    @Autowired
    MyCourseTablesService myCourseTablesService;
    @Autowired
    MyCourseTablesServiceImpl currentProxy;

    @Override
    @Transactional
    public XcChooseCourseDto addChooseCourse(String userId, Long courseId) {
        //查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        String charge = coursepublish.getCharge();

        XcChooseCourse course=null;
        if("201000".equals(charge)){
            //添加免费课程
           course= addFreeCourse(userId,coursepublish);
           //添加到我的课程表
          XcCourseTables xcCourseTables= addCourseTables(course);
        }else {
            //添加收费课程
           course=addchargeCourse(userId,coursepublish);
        }
        //课程收费标准
        XcChooseCourseDto xcChooseCourseDto = new XcChooseCourseDto();

        BeanUtils.copyProperties(course,xcChooseCourseDto);
        XcCourseTablesDto learningStatus = getLearningStatus(userId, courseId);
        xcChooseCourseDto.setLearnStatus(learningStatus.getLearnStatus());


        return xcChooseCourseDto;
    }

    @Override
    public XcCourseTablesDto getLearningStatus(String userId, Long courseId) {
        XcCourseTables xcCourseTables = getXcCourseTables(userId, courseId);
        if(xcCourseTables==null){
            XcCourseTablesDto xcCourseTablesDto = new XcCourseTablesDto();
            //没有选课或选课后没有支付
            xcCourseTablesDto.setLearnStatus("702002");
            return xcCourseTablesDto;
        }
        XcCourseTablesDto xcCourseTablesDto = new XcCourseTablesDto();
        BeanUtils.copyProperties(xcCourseTables,xcCourseTablesDto);
        boolean before = xcCourseTables.getValidtimeEnd().isBefore(LocalDateTime.now());
        if(!before){
            xcCourseTablesDto.setLearnStatus("702001");
            return xcCourseTablesDto;
        }else {
            xcCourseTablesDto.setLearnStatus("702003");
            return xcCourseTablesDto;
        }
    }

    @Override
    public PageResult<XcCourseTables> mycoursetables(MyCourseTableParams params) {
        int pagenum = params.getPage();
        int pageSize = params.getSize();
        Page<XcCourseTables> page = new Page<>(pagenum, pageSize);

        LambdaQueryWrapper<XcCourseTables> xcCourseTablesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<XcCourseTables> xcCourseTablesPage = xcCourseTablesMapper.selectPage(page, xcCourseTablesLambdaQueryWrapper);
        long total = xcCourseTablesPage.getTotal();
        List<XcCourseTables> records = xcCourseTablesPage.getRecords();



        return new PageResult<>(records,total,pagenum,pageSize);
    }

    private XcCourseTables addCourseTables(XcChooseCourse course) {
        String status = course.getStatus();
        if(!"701001".equals(status)){
            throw new XueChengPlusException("选课未成功，无法添加到课程表");
        }
       XcCourseTables xcCourseTables= getXcCourseTables(course.getUserId(), course.getCourseId());
        if(xcCourseTables!=null){
            return xcCourseTables;
        }
        XcCourseTables xcCourseTablesNew = new XcCourseTables();
        xcCourseTablesNew.setChooseCourseId(course.getId());
        xcCourseTablesNew.setUserId(course.getUserId());
        xcCourseTablesNew.setCourseId(course.getCourseId());
        xcCourseTablesNew.setCompanyId(course.getCompanyId());
        xcCourseTablesNew.setCourseName(course.getCourseName());

        xcCourseTablesNew.setCreateDate(LocalDateTime.now());
        xcCourseTablesNew.setValidtimeStart(course.getValidtimeStart());
        xcCourseTablesNew.setValidtimeEnd(course.getValidtimeEnd());
        xcCourseTablesNew.setCourseType(course.getOrderType());
        xcCourseTablesMapper.insert(xcCourseTablesNew);
        return xcCourseTablesNew;
    }

    private XcCourseTables getXcCourseTables(String userId, Long courseId) {
        XcCourseTables xcCourseTables = xcCourseTablesMapper.selectOne(new LambdaQueryWrapper<XcCourseTables>()
                .eq(XcCourseTables::getCourseId, courseId)
                .eq(XcCourseTables::getUserId, userId));
        return xcCourseTables;
    }

    private XcChooseCourse addchargeCourse(String userId,CoursePublish coursePublish) {
        LambdaQueryWrapper<XcChooseCourse> eq = new LambdaQueryWrapper<XcChooseCourse>()
                .eq(XcChooseCourse::getCourseId, coursePublish.getId())
                .eq(XcChooseCourse::getUserId, userId)
                .eq(XcChooseCourse::getOrderType, "700002")
                .eq(XcChooseCourse::getStatus, "701002");
        List<XcChooseCourse> xcChooseCourses = xcChooseCourseMapper.selectList(eq);
        if(xcChooseCourses!=null && xcChooseCourses.size()>0){
            return xcChooseCourses.get(0);
        }
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(coursePublish.getId());
        xcChooseCourse.setCoursePrice(coursePublish.getPrice());
        xcChooseCourse.setUserId(userId);
        xcChooseCourse.setOrderType("700002");
        xcChooseCourse.setStatus("701002");
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setCourseName(coursePublish.getName());
        xcChooseCourse.setCompanyId(coursePublish.getCompanyId());

        xcChooseCourse.setValidDays(coursePublish.getValidDays());
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(coursePublish.getValidDays()));
        xcChooseCourseMapper.insert(xcChooseCourse);

        return xcChooseCourse;
    }

    //向免费课程表添加信息
    private XcChooseCourse addFreeCourse(String userId, CoursePublish coursePublish) {
        LambdaQueryWrapper<XcChooseCourse> queryWrapper = new LambdaQueryWrapper<XcChooseCourse>()
                .eq(XcChooseCourse::getCourseId, coursePublish.getId())
                .eq(XcChooseCourse::getUserId, userId)
                .eq(XcChooseCourse::getOrderType, "700001")
                .eq(XcChooseCourse::getStatus, "701001");
        List<XcChooseCourse> xcChooseCourses = xcChooseCourseMapper.selectList(queryWrapper);
        if(xcChooseCourses!=null && xcChooseCourses.size()>0){
            return xcChooseCourses.get(0);
        }
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(coursePublish.getId());
        xcChooseCourse.setUserId(userId);
        xcChooseCourse.setOrderType("700001");
        xcChooseCourse.setStatus("701001");
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setCourseName(coursePublish.getName());
        xcChooseCourse.setCompanyId(coursePublish.getCompanyId());

        xcChooseCourse.setValidDays(365);
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(365));
        xcChooseCourseMapper.insert(xcChooseCourse);
        return xcChooseCourse;
    }
}
